#include "pch.h"
#include "Datainterface.h"


bool Datainterface::Open(CString FilePath) {
	ifstream in(FilePath, ios::in);
	if (in.is_open()) {
		int Colum;
		in >> Colum;
		for (int i = 0; i < Colum; i++) {
			Info info;
			info.Read(in);
			v_info.push_back(info);
		}
		return true;
	}
	return false;
}

void Datainterface::Add(Info info) {
	v_info.push_back(info);
}

void Datainterface::Del(int lect) {
	v_info.erase(v_info.begin() + lect);
}

void Datainterface::Amend(int lect, Info info) {
	v_info[lect] = info;
}

Info Datainterface::Find(string member) {
	for (int i = 0; i < v_info.size(); i++) {
		if (v_info[i].m_name == member || v_info[i].m_sex == member || v_info[i].m_tele == member || v_info[i].m_address == member || v_info[i].m_mailcode == member || v_info[i].m_mail == member || v_info[i].m_QQ == member || v_info[i].m_type == member) {
			f_info.push_back(v_info[i]);
		}
	}
	if (f_info.empty()) {
		return Info(TEXT(""), TEXT(""), TEXT(""), TEXT(""), TEXT(""), TEXT(""), TEXT(""), TEXT(""));
	}
	else return f_info[0];
}

bool Datainterface::Save(CString FilePath) {
	ofstream out(FilePath);
	if (out.is_open()) {
		out << v_info.size() << endl;
		for (int i = 0; i < v_info.size(); i++) {
			v_info[i].Save(out);
		}
		out.close();
		return true;
	}
	return false;
}





	